//
//  UIViewController+Navigation.m
//
//  Created by Jiaozl on 9/30/16.
//

#import "UIViewController+Navigation.h"

@interface ZLViewControllerOpenItem ()
@property (nonatomic, strong) id vc;
@property (nonatomic, assign) ZLViewControllerOpenType type;
@property (nonatomic, assign) UIModalPresentationStyle presentStyle;

@end

@implementation ZLViewControllerOpenItem
@end

@implementation UIViewController (Navigation)

+ (void)pushViewController:(UIViewController *)vc animated:(BOOL)animate {
   UIViewController *targetVC = [self currentViewController];
   if (targetVC) {
       [targetVC.navigationController pushViewController:vc animated:YES];
   } else {
       dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.75 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
           UIViewController *targetVC = [self currentViewController];
           [targetVC.navigationController pushViewController:vc animated:YES];
       });
   }
}

+ (void)presentViewController:(UIViewController *)vc animated:(BOOL)animated {
    [self presentViewController:vc animated:animated completion:nil];
}

+ (void)notFullScreenPresentViewController:(UIViewController *)vc animated:(BOOL)animated {
    [self notFullScreenPresentViewController:vc animated:animated completion:nil];
}

+ (void)notFullScreenPresentViewController:(UIViewController *)vc animated:(BOOL)animated completion:(void (^)(void))completion {
    [[self currentViewController] presentViewController:vc animated:animated completion:completion];
}

+ (void)notFullScreenOpenViewController:(UIViewController *)vc animated:(BOOL)animated {
    UIViewController *currentVC = [self currentViewController];
    UINavigationController *nav = currentVC.navigationController;
    if (nav) {
        [nav pushViewController:vc animated:animated];
    } else {
        [currentVC presentViewController:vc animated:animated completion:nil];
    }
}

+ (void)openViewController:(UIViewController *)vc animated:(BOOL)animated {
    [self openViewController:vc animated:animated completion:nil];
}


+ (void)presentViewController:(UIViewController *)vc animated:(BOOL)animated completion:(void (^)(void))completion {
    vc.modalPresentationStyle = UIModalPresentationFullScreen;
    [[self currentViewController] presentViewController:vc animated:animated completion:completion];
}

+ (void)openViewController:(UIViewController *)vc animated:(BOOL)animated completion:(OpenVCCompletionHandler)completion {
    UIViewController *currentVC = [self currentViewController];
    UINavigationController *nav = currentVC.navigationController;
    if (nav) {
        [nav pushViewController:vc animated:animated];
    } else {
        ZLViewControllerOpenItem *model          = [ZLViewControllerOpenItem new];
        vc.modalPresentationStyle = UIModalPresentationFullScreen;
        model.type              = ZLViewControllerOpenTypePresent;
        model.presentStyle      = UIModalPresentationFullScreen;
        model.vc                = vc;
        if (completion) {
            [currentVC presentViewController:vc animated:animated completion:^{
                if (completion) {
                    completion(model);
                }
            }];
        } else {
            [currentVC presentViewController:vc animated:animated completion:nil];
        }
    }
}

+ (UIViewController *)currentViewController {
    UIViewController *top = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (top.presentedViewController) {
        top = top.presentedViewController;
    }
    return [[self class] currentViewControllerWithRootViewController:top];
}

+ (UIViewController*)currentViewControllerWithRootViewController:(UIViewController*)rootViewController {
    if ([rootViewController isKindOfClass:[UITabBarController class]]) {
        UITabBarController* tabBarController = (UITabBarController*)rootViewController;
        return [self currentViewControllerWithRootViewController:tabBarController.selectedViewController];
    } else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController* navigationController = (UINavigationController*)rootViewController;
        return [self currentViewControllerWithRootViewController:navigationController.visibleViewController];
    } else if (rootViewController.presentedViewController) {
        UIViewController* presentedViewController = rootViewController.presentedViewController;
        return [self currentViewControllerWithRootViewController:presentedViewController];
    } else {
        return rootViewController;
    }
}


@end
